﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.Script.Serialization;
using System.Configuration;

namespace WindowsFormsApplication1
{
    public partial class Registered : Form
    {
        public Registered(String appId)
        {
            InitializeComponent();
            this.appId = appId;
        }
        String appId;
        private void Registered_Load(object sender, EventArgs e)
        {
            this.panel1.BackColor = Color.Transparent;
            this.label1.BackColor = Color.Transparent;
            this.panel1.Location = new Point((this.Width / 2) - (this.panel1.Width / 2), (this.Height / 2) - this.panel1.Height);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String identifier = this.textBox1.Text.Trim();
            try
            {
                String result = Cryptography.AES_decrypt(identifier, ref appId);
                Identifier identifierO = new JavaScriptSerializer().Deserialize<Identifier>(result);
                identifierO.preTime = DateTime.Now;
                if (identifierO.appId.Equals(appId) && DateTime.Compare(identifierO.endTime, DateTime.Now) > 0)
                {
                    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
                    config.AppSettings.Settings["Identifier"].Value = identifier;
                    config.Save(ConfigurationSaveMode.Modified);
                    //String entity = System.IO.File.ReadAllText(Application.StartupPath + "\\csdra.exe");
                    System.IO.File.WriteAllText(Application.StartupPath + "\\csdra.exe", Cryptography.AES_encrypt(new JavaScriptSerializer().Serialize(identifierO),this.appId));
                    result = "";
                    identifierO = null;
                    this.Close();
                }
            }
            catch (Exception)
            {

                MessageBox.Show("注册码错误");
            }
        }
    }
}
